﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace DusonFramework.Core.Web.Office
{
    public class Export
    {
        /// <summary>
        /// 导出数据到execl 
        /// </summary>
        /// <param name="gv">数据集对象</param>
        public static void ExportToExecl(GridView gv)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");

            //   如果设置为GetEncoding("GB2312");导出的文件将会出现乱码 
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
            HttpContext.Current.Response.ContentType = "application/ms-excel"; // 设置输出文件类型为excel文件。     
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            gv.RenderControl(oHtmlTextWriter);

            HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

    }
}
